/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.param.service.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import net.comm.common.GlbFinal;
import net.comm.common.ReturnMessage;
import net.comm.param.service.impl.SequenceMgr;
import net.comm.common.common;


import net.comm.param.bean.BaseProperty;
import net.comm.param.db.DbPropertyService;
import net.comm.param.db.PropertyServiceSqlMysql;
import net.comm.param.web.form.PropertyForm;
import net.comm.page.PageCtr;

import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class PropertyServiceImpl {

	static Logger logger = Logger.getLogger(PropertyServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(PropertyForm theForm,  ReturnMessage msg) 
	{
		List list = null;

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql1();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, null));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[1] = page.getPos();
			fields2[2] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();

			
			list = bSqlServ.dbRead(cn, sql, fields2, new DbPropertyService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List adlist(PropertyForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;
		Statement stmt=null;
		ResultSet rs=null;
		try
		{
			cn = DbcpPool.getConnection();
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			
			StringBuffer sb = new StringBuffer(" where 1=1");
			if(theForm.getQryAssortID() > 0){
				
				sb.append(" and AssortID = ");
				sb.append(theForm.getQryAssortID());
				sb.append(" ");
			}
			if(theForm.getQryParentID() > 0){
				
				sb.append(" and ParentID = ");
				sb.append(theForm.getQryParentID());
				sb.append(" ");
			}
			sb.append(" Order by AssortID, DisNO,PropID ");
			
			//查询记录数
			stmt = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			String sql = servSql.getCountSql1();

			logger.debug(sql+sb.toString());
			rs=stmt.executeQuery(sql+sb.toString());
			int nRows = 0;
			if (rs.next())
	           nRows = rs.getInt("COUNT");
			else
			{
	            rs.close();
	            stmt.close();
	            DbcpPool.returnConnection(cn);
			    return list;
			}
			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(nRows);
			page.caculate();

			sb.append(" limit ");
			sb.append(page.getPos()-1);
			sb.append(", ");
			sb.append(page.getPageSize());
			
			 //取记录
			 sql = servSql.getRecbyPageSql();
			 logger.debug(sql+sb.toString());
			 DbPropertyService dbservice = new DbPropertyService();
			 
			 rs=stmt.executeQuery(sql+sb.toString());
			 while (rs.next())
			 {
				try
				{
					Object obj = dbservice.readSqlResultRecord(rs);
					if (obj != null) list.add(obj);
				}
				catch (Throwable t)
				{
					logger.error(t.getMessage());
				}		
			 }
			 rs.close();
			 stmt.close();
		     DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				DbcpPool.returnConnection(cn);
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}			
	    }finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}			
			DbcpPool.returnConnection(cn);
		}	
		return list;
	}
	
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public String  getPropNames(int assortID, String propIDs) 
	{
		Connection cn = null;
		StringBuffer  sb = new StringBuffer("");
		try
		{
			cn = DbcpPool.getConnection();
			
			List list = null;

			String[] ids = propIDs.split(",");

			//取得FORM中传入的查询参数
			Object[] fields11 = new Object[11];
			
			fields11[0] = assortID;
			int len = ids.length;
			if(len < 11){
				for(int i=0; i<len; i++ ){
					
					fields11[i+1] = ids[i];
				}
			}
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getRecbyIDsSql();			
					
			list = bSqlServ.dbRead(cn, sql, fields11, new DbPropertyService());
			
			if(list.size() ==0){
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return sb.toString();
			}
			len = list.size();
			BaseProperty base = new BaseProperty();
			for(int i=0; i <len; i++){
				
				base = (BaseProperty)list.get(i);
				sb.append(base.getPropname());
				sb.append(" ");
			}
			
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return sb.toString();
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return sb.toString();
	}
	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(PropertyForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = theForm.getAssortID();
			fields2[1] = theForm.getPropID();
			
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields2, new DbPropertyService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			
			BaseProperty base = (BaseProperty)list.get(0);
				
			
			BeanUtils.copyProperties(theForm, base); 
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	
	/**
	 * 添加属性到表中
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(PropertyForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			List list = null;

			//取得FORM中传入的查询参数
			Object[] fields1 = new Object[1];
			
			//增加新的分类
			if(!theForm.getAssortname().equals("")){

				fields1[0] = theForm.getAssortname();
				
				String sql = servSql.getRecbyAssortNameSql();			
				
				list = bSqlServ.dbRead(cn, sql, fields1, new DbPropertyService());
				if(list.size() > 0){
					common.setAppFailMsg(msg , "该分类已经存在，请检查！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return;		
				}			
				//分类ID
				theForm.setAssortID(SequenceMgr.generateSeq(GlbFinal.SEQ_INITDATE,GlbFinal.SEQ_ASSORTID));	
				theForm.setPropID(1);
			}else{
				fields1[0] = theForm.getAssortID();
				
				String sql = servSql.getRecbyAssortIDSql();			
				
				list = bSqlServ.dbRead(cn, sql, fields1, new DbPropertyService());
				if(list.size() == 0){
					theForm.setPropID(1);
				}else{
					BaseProperty base = (BaseProperty)list.get(0);
					
					theForm.setPropID(base.getPropID() + 1);
				}
			}

			//取得FORM中传入的查询参数
			Object[] fields = new Object[9];
			
			fields[0] = theForm.getAssortID();
			fields[1] = theForm.getPropID();
			fields[2] = theForm.getPropname();
			fields[3] = theForm.getAssortname();
			fields[4] = theForm.getParentID();
			fields[5] = theForm.getDisNO();
			fields[6] = theForm.getExichiflag();
			fields[7] = theForm.getUserID();
			fields[8] = DatetimeUtil.getDatetime();
			
			String sql = servSql.getInsertSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 添加属性失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(PropertyForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = theForm.getAssortID();
			fields2[1] = theForm.getPropID();
			
			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			String sql = servSql.getDeleteSql();
			
			boolean  res = bSqlServ.dbWrite(cn, sql, fields2);
			if(!res){
				common.setAppFailMsg(msg , " 删除属性记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about info" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(PropertyForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			PropertyServiceSqlMysql servSql = new PropertyServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			//取得FORM中传入的查询参数
			Object[] fields = new Object[9];
			
			fields[0] = theForm.getPropname();
			fields[1] = theForm.getAssortname();
			fields[2] = theForm.getParentID();
			fields[3] = theForm.getDisNO();
			fields[4] = theForm.getExichiflag();
			fields[5] = theForm.getUserID();
			fields[6] = DatetimeUtil.getDatetime();
			fields[7] = theForm.getAssortID();
			fields[8] = theForm.getPropID();
			
			String sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新属性记录失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about info" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

}
